//
//  ViewController.m
//  No15.2 map
//
//  Created by Bc_Ltf on 15/1/13.
//  Copyright (c) 2015年 Bc_Ltf. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<MKMapViewDelegate>

@end

@implementation ViewController
@synthesize textTextField;
@synthesize mapView;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    mapView.delegate=self;
    mapView.mapType=MKMapTypeHybrid;// 清华大学
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*-----------------------------------点击按钮加载很多------------------------------------*/
- (IBAction)pressLocation:(id)sender {
    // 判断是否有输入
    if (textTextField.text==nil||[textTextField.text length]==0) {
        return;
    }
    CLGeocoder *geocoder=[CLGeocoder new];
    
    [geocoder geocodeAddressString:textTextField.text completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"查询记录数：%i",(int)[placemarks count]);
        if([placemarks count]>0)
        {
            [mapView removeAnnotations:mapView.annotations];
        }
        for(int i=0;i<[placemarks count];i++)
        {
            CLPlacemark *placemark =placemarks[i];
            
            // 关闭键盘
            // resign 辞职放弃，委托
            [textTextField resignFirstResponder];
            
            //调整地图比例 region:地区范围部位
            MKCoordinateRegion viewRegion=MKCoordinateRegionMakeWithDistance(placemark.location.coordinate, 1000, 1000);
            
            // animate：又生气的，激活
            // 设置重新显示区域
            [mapView setRegion:viewRegion animated:YES];
            
            MapLocation *annotation=[MapLocation new];
            // thorough:十分的彻底的 fare:经营 thoroughfare :大道
            //
            annotation.streetAddressString=placemark.thoroughfare;
            NSLog(@"%@",annotation.streetAddressString);
            annotation.stateString=placemark.administrativeArea;
            annotation.cityString=placemark.locality;
            annotation.zipString=placemark.postalCode;
            annotation.coordinate=placemark.location.coordinate;
            
            
            
            [mapView addAnnotation:annotation];
            
        }
    }];
    
}

- (IBAction)mapStand:(id)sender {
    mapView.mapType=MKMapTypeStandard;// 标准地图
}

- (IBAction)mapSatellite:(id)sender {
    mapView.mapType=MKMapTypeSatellite; // 卫星地图
}

- (IBAction)mapHybrid:(id)sender {
    mapView.mapType=MKMapTypeHybrid;  // 混合地图
}

/*-----------------------------------实现mapviewdelegate协议方法------------------------------------*/
// -（某标注视图）地图视图：（某地图视图）为标注准备的视图：某实现了mkannotation的对象
- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>) annotation {
    
    MKPinAnnotationView *annotationView
    // reusable：可复用的 通过标识列出可复用的注释视图
    = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOTATION"];
    if(annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                         reuseIdentifier:@"PIN_ANNOTATION"];
    }
    
    annotationView.pinColor = MKPinAnnotationColorPurple;// 紫色的大头真注释
    // drop 滴下落下
    //A Boolean value indicating whether the annotation view is animated onto the screen.
    // 一个布尔值表面这个注释视图是否在屏幕上激活了
    
    // When this property is YES, the map view animates the appearance of pin annotation views by making them appear to drop onto the map at the target point. This animation occurs whenever the view transitions from offscreen to onscreen.
    
    // 当这个值是YES的的时候，地图视图上激活了大头针注释视图，通过让他们出现并且插在目标点的方式。
    
    annotationView.animatesDrop = YES;
    annotationView.canShowCallout = YES;// 显示附加信息
    
    return annotationView;
}
// 刷新地图定位  根据用户位置刷新地图位置
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    mapView.centerCoordinate=userLocation.location.coordinate;
}

// 地图视图加载失败
-(void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"error :%@",[error description]);
}



@end
